package com.dc.service.handler;

import com.dc.constat.DeviceConstants;
import com.dc.enums.ServiceIdType;
import com.dc.enums.SystemClass;
import com.dc.factory.DeviceFactory;
import com.dc.feign.BaseApiServiceFeign;
import com.dc.feign.SystemApiServiceFeign;
import com.dc.model.Device;
import com.dc.ro.CommonParams;
import com.dc.service.DeviceService;
import com.dc.util.RedisUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

/**
 * @author zhang
 * 报警消息下发指令
 */
@Service
@Slf4j
@AllArgsConstructor
public class AlarmStatusHandler implements DeviceService, InitializingBean {

    private final SystemApiServiceFeign systemApiServiceFeign;
    private final BaseApiServiceFeign baseApiServiceFeign;
    private final RedisUtil redisUtil;

    @Override
    public void deviceHandler(String payLoad, String mqttId, CommonParams commonParams) {
        // 逻辑处理，报警返回数据给前端
        if (null == commonParams || null == commonParams.getSerialNumber()) {
            return;
        }
        systemApiServiceFeign.sendWebsocketMessage(payLoad, null);
        // 存数据
        /*redisUtil.hset(DeviceConstants.DEVICE_XF_XMZ_BJ + commonParams.getSerialNumber(),
                String.valueOf(commonParams.getDeviceMsgId()), payLoad);*/
        Device device = new Device();
        device.setSubSystem(SystemClass.SIEMENS_ACQUISITION.getName());
        device.setDeviceFactory(SystemClass.SIEMENS_ACQUISITION.getDesc());
        device.setName(String.format("%s%s%s", SystemClass.SIEMENS_ACQUISITION.getRemarkName(),
                DeviceConstants.DEVICE_NAME_FORMAT,
                commonParams.getSerialNumber()));
        BeanUtils.copyProperties(commonParams, device);
        baseApiServiceFeign.insertDevice(device);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceFactory.register(ServiceIdType.DEVICE_ALARM_STATE.getType(), this);
    }
}
